﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using System.Text;

public partial class main121 : System.Web.UI.Page
{
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
        UpdateTowerTable();

    }
    protected void UpdateTowerTable()
    {
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("塔杆名称");
        dataTable.Columns.Add("采样时间");
        dataTable.Columns.Add("工作温度");
        dataTable.Columns.Add("电池电压");
        dataTable.Columns.Add("在线状态");
        dataTable.Columns.Add("综合告警");
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        List<String> nameList = towerDAOs.getTowerNameList(Session["LineName"].ToString());
        if (nameList.Count > 0)
        {
            for (int i = 0; i < nameList.Count; i++)
            {
                Tower tower = towerDAOs.getTowerByName(Session["LineName"].ToString(),nameList[i]);
                if (tower != null)
                {
                    dataTable.Rows.Add(new object[] { tower.TowerName,tower.Time,tower.Temperature,tower.BatteryVoltage,tower.OnlineStatus,tower.Alarm});
                }
            }

        }
        GridView1.DataSource = dataTable;
        GridView1.DataBind();
    }
    protected void Detail_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Equals("TowerDetail"))
        {
            Session["TowerName"] = e.CommandArgument;
            Response.Redirect("./main12_L3.aspx");

        }
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=ExcelFile.csv");
        Response.Charset = "gb2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.ContentType = "application/text";
        StringBuilder sb = new StringBuilder();
        DataTable dt = new DataTable();
        dt = (DataTable)GridView1.DataSource;
        for (int k = 0; k < dt.Columns.Count; k++)
        {
            sb.Append(dt.Columns[k].ColumnName + ',');
        }
        sb.Append(Environment.NewLine);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int k = 0; k < dt.Columns.Count; k++)
            {
                //add separator 
                sb.Append(dt.Rows[i][k].ToString() + ',');
            }
            //append new line 
            sb.Append(Environment.NewLine);
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End(); 

    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=TextFile.txt");
        Response.ContentType = "application/vnd.ms-word";
        Response.Charset = "";
        StringBuilder sb = new StringBuilder();
        DataTable dt = new DataTable();
        dt = (DataTable)GridView1.DataSource;
        for (int i = 0; i < dt.Columns.Count; i++)
        {
            sb.AppendFormat("{0,-10}", dt.Columns[i].ColumnName);


        }
        sb.Append(Environment.NewLine);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                sb.AppendFormat("{0,-10}", dt.Rows[i][j].ToString());

            }
            sb.Append(Environment.NewLine);
        }
        Response.Write(sb.ToString());
        Response.End();

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataTable dt = (DataTable)GridView1.DataSource;
            if (dt.Rows[e.Row.RowIndex].ItemArray[5].ToString().Equals("True"))
            {
                e.Row.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {

    }
}
